Public Class frmResourcePicker
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents pnlProperties As System.Windows.Forms.Panel
    Friend WithEvents lblRuleTypeName As System.Windows.Forms.Label
    Friend WithEvents txtResourceTypeName As System.Windows.Forms.TextBox
    Friend WithEvents lstResourceTypes As System.Windows.Forms.ListBox
    Friend WithEvents lblResourceText As System.Windows.Forms.Label
    Friend WithEvents lblAccrueAmount As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents btnCancel As System.Windows.Forms.Button
    Friend WithEvents btnOK As System.Windows.Forms.Button
    Friend WithEvents txtMaxAmount As System.Windows.Forms.TextBox
    Friend WithEvents txtAccrueAmount As System.Windows.Forms.TextBox
    Friend WithEvents txtResourceDescription As System.Windows.Forms.TextBox
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(frmResourcePicker))
        Me.pnlProperties = New System.Windows.Forms.Panel
        Me.txtMaxAmount = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.txtAccrueAmount = New System.Windows.Forms.TextBox
        Me.lblAccrueAmount = New System.Windows.Forms.Label
        Me.txtResourceTypeName = New System.Windows.Forms.TextBox
        Me.lblRuleTypeName = New System.Windows.Forms.Label
        Me.txtResourceDescription = New System.Windows.Forms.TextBox
        Me.lblResourceText = New System.Windows.Forms.Label
        Me.lstResourceTypes = New System.Windows.Forms.ListBox
        Me.btnCancel = New System.Windows.Forms.Button
        Me.btnOK = New System.Windows.Forms.Button
        Me.pnlProperties.SuspendLayout()
        Me.SuspendLayout()
        '
        'pnlProperties
        '
        Me.pnlProperties.BackColor = System.Drawing.SystemColors.ControlLight
        Me.pnlProperties.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlProperties.Controls.Add(Me.txtMaxAmount)
        Me.pnlProperties.Controls.Add(Me.Label2)
        Me.pnlProperties.Controls.Add(Me.txtAccrueAmount)
        Me.pnlProperties.Controls.Add(Me.lblAccrueAmount)
        Me.pnlProperties.Controls.Add(Me.txtResourceTypeName)
        Me.pnlProperties.Controls.Add(Me.lblRuleTypeName)
        Me.pnlProperties.Controls.Add(Me.txtResourceDescription)
        Me.pnlProperties.Controls.Add(Me.lblResourceText)
        Me.pnlProperties.Location = New System.Drawing.Point(160, 8)
        Me.pnlProperties.Name = "pnlProperties"
        Me.pnlProperties.Size = New System.Drawing.Size(192, 252)
        Me.pnlProperties.TabIndex = 2
        '
        'txtMaxAmount
        '
        Me.txtMaxAmount.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtMaxAmount.Location = New System.Drawing.Point(80, 72)
        Me.txtMaxAmount.Name = "txtMaxAmount"
        Me.txtMaxAmount.Size = New System.Drawing.Size(104, 20)
        Me.txtMaxAmount.TabIndex = 3
        Me.txtMaxAmount.Text = "0"
        Me.txtMaxAmount.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 72)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(72, 16)
        Me.Label2.TabIndex = 14
        Me.Label2.Text = "Max Amount"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'txtAccrueAmount
        '
        Me.txtAccrueAmount.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtAccrueAmount.Location = New System.Drawing.Point(79, 40)
        Me.txtAccrueAmount.Name = "txtAccrueAmount"
        Me.txtAccrueAmount.Size = New System.Drawing.Size(104, 20)
        Me.txtAccrueAmount.TabIndex = 2
        Me.txtAccrueAmount.Text = "0"
        Me.txtAccrueAmount.TextAlign = System.Windows.Forms.HorizontalAlignment.Center
        '
        'lblAccrueAmount
        '
        Me.lblAccrueAmount.Location = New System.Drawing.Point(7, 40)
        Me.lblAccrueAmount.Name = "lblAccrueAmount"
        Me.lblAccrueAmount.Size = New System.Drawing.Size(72, 24)
        Me.lblAccrueAmount.TabIndex = 12
        Me.lblAccrueAmount.Text = "Accrue Amount"
        Me.lblAccrueAmount.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'txtResourceTypeName
        '
        Me.txtResourceTypeName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtResourceTypeName.Location = New System.Drawing.Point(79, 8)
        Me.txtResourceTypeName.Name = "txtResourceTypeName"
        Me.txtResourceTypeName.Size = New System.Drawing.Size(104, 20)
        Me.txtResourceTypeName.TabIndex = 1
        Me.txtResourceTypeName.Text = "New Resource"
        '
        'lblRuleTypeName
        '
        Me.lblRuleTypeName.Location = New System.Drawing.Point(7, 11)
        Me.lblRuleTypeName.Name = "lblRuleTypeName"
        Me.lblRuleTypeName.Size = New System.Drawing.Size(72, 16)
        Me.lblRuleTypeName.TabIndex = 10
        Me.lblRuleTypeName.Text = "Type Name"
        Me.lblRuleTypeName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
        '
        'txtResourceDescription
        '
        Me.txtResourceDescription.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtResourceDescription.Location = New System.Drawing.Point(8, 176)
        Me.txtResourceDescription.Multiline = True
        Me.txtResourceDescription.Name = "txtResourceDescription"
        Me.txtResourceDescription.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.txtResourceDescription.Size = New System.Drawing.Size(176, 64)
        Me.txtResourceDescription.TabIndex = 4
        Me.txtResourceDescription.Text = "Enter a description."
        '
        'lblResourceText
        '
        Me.lblResourceText.Location = New System.Drawing.Point(8, 160)
        Me.lblResourceText.Name = "lblResourceText"
        Me.lblResourceText.Size = New System.Drawing.Size(96, 16)
        Me.lblResourceText.TabIndex = 8
        Me.lblResourceText.Text = "Resource Text"
        '
        'lstResourceTypes
        '
        Me.lstResourceTypes.Location = New System.Drawing.Point(8, 8)
        Me.lstResourceTypes.Name = "lstResourceTypes"
        Me.lstResourceTypes.Size = New System.Drawing.Size(144, 251)
        Me.lstResourceTypes.TabIndex = 0
        '
        'btnCancel
        '
        Me.btnCancel.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnCancel.Location = New System.Drawing.Point(288, 272)
        Me.btnCancel.Name = "btnCancel"
        Me.btnCancel.Size = New System.Drawing.Size(56, 23)
        Me.btnCancel.TabIndex = 6
        Me.btnCancel.Text = "Cancel"
        '
        'btnOK
        '
        Me.btnOK.FlatStyle = System.Windows.Forms.FlatStyle.Flat
        Me.btnOK.Location = New System.Drawing.Point(224, 272)
        Me.btnOK.Name = "btnOK"
        Me.btnOK.Size = New System.Drawing.Size(56, 23)
        Me.btnOK.TabIndex = 5
        Me.btnOK.Text = "OK"
        '
        'frmResourcePicker
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(354, 302)
        Me.Controls.Add(Me.btnCancel)
        Me.Controls.Add(Me.btnOK)
        Me.Controls.Add(Me.lstResourceTypes)
        Me.Controls.Add(Me.pnlProperties)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
        Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
        Me.MaximizeBox = False
        Me.MinimizeBox = False
        Me.Name = "frmResourcePicker"
        Me.ShowInTaskbar = False
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
        Me.Text = "Resource"
        Me.pnlProperties.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private mvarCancelPressed As Boolean = False
    Private ptrCGD As cmplib01.CampaignerGameData
    Private ptrItem As cmplib01.Resource

    Private Const DEFAULT_ACCRUE As Integer = 1
    Private Const DEFAULT_MAX As Integer = 15

    Public Sub New(ByRef cgd As cmplib01.CampaignerGameData, ByRef res As cmplib01.Resource)
        Me.New()
        Dim de As DictionaryEntry
        Dim key As String

        ptrCGD = cgd
        ptrItem = res

        If ptrCGD.ResourceTypes_g.Count = 0 Then
            MsgBox("There are no resource types defined. You must have resource types defined before you may add a resource to an objective.", MsgBoxStyle.Information, "No Resources Defined")
            mvarCancelPressed = True
            Me.Hide()
            Exit Sub
        End If

        For Each de In ptrCGD.ResourceTypes_g.HT
            key = de.Key
            lstResourceTypes.Items.Add(ptrCGD.ResourceTypes_g.Item(key))
        Next
        txtResourceDescription.Enabled = True
        lstResourceTypes.SelectedIndex = 0
        PopulateProperties()

    End Sub

    Private Sub PopulateProperties()
        Dim selItem As cmplib01.ResourceType
        Try
            selItem = lstResourceTypes.SelectedItem
        Catch
            Exit Sub
        End Try

        txtResourceTypeName.Text = selItem.ResourceName
        txtAccrueAmount.Text = 0
        txtMaxAmount.Text = 0
        txtResourceDescription.Text = selItem.ResourceDescription

    End Sub

    Public ReadOnly Property CancelPressed() As Boolean
        Get
            Return mvarCancelPressed
        End Get
    End Property

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        ' once the use clicked on a resource and made the changes to accrue and max amounts, we push that data into
        ' the provided rule. :P simple as pie.
        ' don't forget to set the pointer... doh!
        Dim usesDefault As Boolean = False ' if any data is incorrect, it will flag the use of a default value

        Try
            ptrItem.AccrueAmount = CType(txtAccrueAmount.Text, Int16)
        Catch
            ptrItem.AccrueAmount = DEFAULT_ACCRUE
            usesDefault = True
        End Try

        Try
            ptrItem.MaxAmount = CType(txtMaxAmount.Text, Int16)
        Catch
            ptrItem.MaxAmount = DEFAULT_MAX
            usesDefault = True
        End Try

        Dim selItem As cmplib01.ResourceType
        Try
            selItem = lstResourceTypes.SelectedItem
            ptrItem.ResourceTypeID = selItem.ResourceTypeID
            ptrItem.ResourceName = selItem.ResourceName

        Catch
            mvarCancelPressed = True
            ' pretend that the cancel button was pressed.
            Me.Hide()
            Exit Sub
        End Try

        mvarCancelPressed = False
        Me.Hide()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        mvarCancelPressed = True
        Me.Hide()
    End Sub

    Private Sub lstResourceTypes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstResourceTypes.SelectedIndexChanged
        PopulateProperties()
    End Sub
End Class
